<div class="layui-fluid" id="VIEW-list-table" lay-title="定时任务">
    <div class="layui-row layui-col-space12">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body nepadmin-table-full">
                    <div class="layui-btn-container nepadmin-pad-t10 nepadmin-pad-l10">
                        <div class="layui-btn layui-btn-sm add">添加任务</div>
                    </div>
                    <table id="list-table-job" lay-filter="list-table-job"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['admin', 'table', 'form', 'jquery'], function () {
        var $ = layui.jquery
            , layer = layui.layer
            , form = layui.form
            , admin = layui.admin
            , table = layui.table;
        var view = $('#VIEW-list-table');

        var tableFilter = 'list-table-job';
        table.render({
            elem: '[lay-filter="' + tableFilter + '"]',
            //api: 'getGoods',
            url: '/sysJob/page?token=' + admin.loginToken,
            height: 'full-165',
            cols: [[
                {title: '', type: 'checkbox', fixed: true, width: 60},
                {title: '任务名', field: 'jobName'},
                {title: '任务组', field: 'jobGroup'},
                {title: '类名', field: 'className', minWidth: 300},
                {title: '表达式', field: 'cronExpression'},
                {
                    title: '状态', width: 80, templet: function (d) {
                        var status = {
                            1: {title: '暂停', color: 'blue'},
                            0: {title: '正常', color: 'orange'}
                        }[d.status];
                        return '<span class="layui-badge layui-bg-' + status.color + '">' + status.title + '</span>';
                    }
                },
                {
                    title: '操作', width: 220, align: 'center', templet: function (d) {
                        var jobBtn = '';
                        if (d.status == 1) {
                            jobBtn = '<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="resume">启用</a>';
                        } else {
                            jobBtn = '<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="pause">暂停</a>';
                        }
                        var runBtn = '<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="run">执行</a>';
                        var editBtn = '<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>';
                        var delBtn = '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
                        return jobBtn + runBtn + editBtn + delBtn;
                }
                }
            ]]
        });

        table.on('tool(' + tableFilter + ')', function (obj) {
            if (obj.event === 'resume') {//启用
                admin.post({
                    url: '/sysJob/resumeJob/'+obj.data.id+'?_method=PUT',
                    contentType: "application/json",
                    success: function (res) {
                        if (res.code == 0) {
                            layer.msg(res.msg);
                            layer.closeAll();
                            table.reload('list-table-job', {
                                url: '/sysJob/page?token=' + admin.loginToken,
                                page: {
                                    curr: 1
                                }
                            })
                        } else {
                            layer.msg(res.msg);
                        }
                    }
                });
            } else if (obj.event === 'pause') {//暂停
                admin.post({
                    url: '/sysJob/pauseJob/'+obj.data.id+'?_method=PUT',
                    contentType: "application/json",
                    success: function (res) {
                        if (res.code == 0) {
                            layer.msg(res.msg);
                            layer.closeAll();
                            table.reload('list-table-job', {
                                url: '/sysJob/page?token=' + admin.loginToken,
                                page: {
                                    curr: 1
                                }
                            })
                        } else {
                            layer.msg(res.msg);
                        }
                    }
                });
            } else if (obj.event === 'run') {//执行
                admin.post({
                    url: '/sysJob/run/'+obj.data.id+'?_method=PUT',
                    contentType: "application/json",
                    success: function (res) {
                        if (res.code == 0) {
                            layer.msg(res.msg);
                            layer.closeAll();
                        } else {
                            layer.msg(res.msg);
                        }
                    }
                });
            } else if (obj.event === 'del') {//删除行

            } else if (obj.event === "edit") {
                admin.popup({
                        url: '/sys/job/sysJobEdit',
                        area: ['650px', '450px'],
                        data : {
                            table:table,
                            jobId : obj.data.id
                        }
                    }
                )
                return false
            }
        });

        view.find('.add').click(function () {
            admin.popup({
                    url: '/sys/job/sysJobEdit',
                    area: ['650px', '450px'],
                    data : {
                        table:table
                    }
                }
            )
            return false
        })

        view.find('.table-action').click(function () {
            var checkedCount = table.checkStatus(tableFilter).data.length;
            if (checkedCount == 0) {
                layer.msg('请先选择某行');
            } else {
                layer.msg($(this).html() + checkedCount + '项');
            }
        })

    })
</script>